/***********************************************************************************************************************
 * ############################################# Application AdewebLite #############################################
 * AdewebLite Android est une application permettant de consulter des emplois du temps fournis par l'université UHP
 * avec ou sans connexion internet (informations gardées en mémoire à chaque synchronisation). Et utilisant la web
 * API ADE pour collecter les données.
 * @author Andrianjafintrimo Victor, Zanette Sébastien
 * 
 * Basée sur l'application du même nom, utilisant le flux RSS (données à traiter), de :
 * @author Schnell Michaël, Zanette Sébastien
 * **********************************************************************************************************************
 */

package ade.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 *
 * @author Zanette Sébastien, @author Andrianjafintrimo Victor
 */
public class CreateDatabases extends SQLiteOpenHelper {
    
        //Attributs de la Table SCHEDULE
        public static final String TABLE_SCHEDULE = "SCHEDULE";
        public static final String COL_NAME = "NAME";
        public static final String COL_PROJECT_ID = "PROJECTID";
        public static final String COL_RESSOURCE_ID = "RESSOURCEID";
        public static final String COL_STUDENT_NUM = "STUDENTNUM";
        public static final String COL_SUB_PERIOD = "PERIOD";
        public static final String COL_LAST_MODIF = "LASTMODIF";
        public static final String COL_STATUS = "STATUS";
        public static final String COL_CREATED = "CREATED";

        //Attributs de la Table LESSON
        public static final String TABLE_LESSON = "LESSON";
        public static final String COL_LESSON_ID = "LESSON_ID";
        public static final String COL_TITLE = "TITLE";
        public static final String COL_TIME = "TIME";
        public static final String COL_SCHEDULE = "SCHEDULE";
        public static final String COL_TYPE = "TYPE";
        public static final String COL_CHANGE = "CHANGE";
        public static final String COL_ROOM = "ROOM";
        public static final String COL_GROUP = "GROUPS";
        public static final String COL_TEACHER = "TEACHER";
        public static final String COL_COLOR = "COLOR";
        public static final String COL_NOTE = "NOTE";
        
        //Attribut de le Table LESSON_HISTORY
        public static final String TABLE_LESSON_HISTORY = "LESSON_HISTORY";
                
        //Attributs de la Table STUDENT_NUMBER
        public static final String TABLE_STUDENT_NUMBER = "STUDENT_NUMBER";
        public static final String COL_STUDENT_NUMBER = "STUDENT_NUMBER";


private static final String CREATE_SCHEDULE_BDD = 
                        "CREATE TABLE IF NOT EXISTS " + TABLE_SCHEDULE + " ("
                                        + COL_NAME + " TEXT PRIMARY KEY, "
                                        + COL_PROJECT_ID + " INTEGER NOT NULL," 
                                        + COL_RESSOURCE_ID + " TEXT," 
                                        + COL_STUDENT_NUM + " TEXT,"
                                        + COL_SUB_PERIOD + " INTEGER NOT NULL, "
                                        + COL_LAST_MODIF + " DATETIME, "
                                        + COL_STATUS + " INT NOT NULL, "
                                        + COL_CREATED + " DATETIME NOT NULL)";

        private static final String CREATE_LESSON_BDD = 
                        "CREATE TABLE IF NOT EXISTS " + TABLE_LESSON + " ("
                                        + COL_LESSON_ID + " INTEGER NOT NULL, "
                                        + COL_TITLE + " VARCHAR(100) NOT NULL," 
                                        + COL_TIME + " DATETIME NOT NULL," 
                                        + COL_SCHEDULE+ " VARCHAR(30) NOT NULL, "
                                        + COL_TYPE+ " INTEGER NOT NULL, "
                                        + COL_CHANGE+ " INTEGER NOT NULL, "
                                        + COL_LAST_MODIF + " DATETIME, " 
                                        + COL_ROOM + " VARCHAR(20), "
                                        + COL_GROUP + " VARCHAR(30), "
                                        + COL_TEACHER + " VARCHAR(20),"
                                        + COL_COLOR + " VARCHAR(15),"
                                        + COL_NOTE + " VARCHAR(100),"
                                        + "PRIMARY KEY (" + COL_LESSON_ID + ", " + COL_SCHEDULE + "));" ;
                
        private static final String CREATE_STUDENT_NUMBER_BDD = 
                        "CREATE TABLE IF NOT EXISTS " + TABLE_STUDENT_NUMBER + " ("
                                        + COL_STUDENT_NUMBER + " TEXT PRIMARY KEY, "
                                        + COL_RESSOURCE_ID + " TEXT NOT NULL);";
        
       private static final String CREATE_LESSON_HISTORY_BDD = 
                        "CREATE TABLE IF NOT EXISTS " + TABLE_LESSON_HISTORY + " ("
                                        + COL_LESSON_ID + " INTEGER NOT NULL, "
                                        + COL_TITLE + " VARCHAR(100) NOT NULL," 
                                        + COL_TIME + " DATETIME NOT NULL," 
                                        + COL_SCHEDULE+ " VARCHAR(30) NOT NULL, "
                                        + COL_TYPE+ " INTEGER NOT NULL, "
                                        + COL_CHANGE+ " INTEGER NOT NULL, "
                                        + COL_LAST_MODIF + " DATETIME, " 
                                        + COL_ROOM + " VARCHAR(20), "
                                        + COL_GROUP + " VARCHAR(30), "
                                        + COL_TEACHER + " VARCHAR(20),"
                                        + COL_COLOR + " VARCHAR(15),"
                                        + COL_NOTE + " VARCHAR(100),"
                                        + "PRIMARY KEY (" + COL_LESSON_ID + ", " + COL_SCHEDULE + "));" ;

        /**
         * Nouvelle instance, servant à créer ou initialiser la base de données.
         * @param context contexte
         * @param name nom de la base de données
         * @param factory null
         * @param version numérode version de la base de données
         */
	public CreateDatabases(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version) ;
		this.onCreate(this.getWritableDatabase()) ;
	}

        /**
         * Création de la Base de données et des tables SCHEDULE, LESSON, STUDENT_NUMBER et HISTORY
         * @param db type de base de données
         */
	@Override
	public void onCreate(SQLiteDatabase db) {
		//on créé la table SCHEDULE à partir de la requête écrite dans la variable CREATE_SCHEDULE_BDD
                db.execSQL(CREATE_STUDENT_NUMBER_BDD);
		db.execSQL(CREATE_SCHEDULE_BDD);
		db.execSQL(CREATE_LESSON_BDD);
                db.execSQL(CREATE_LESSON_HISTORY_BDD);
	}

        /**
         * Met à jour la base de données, afin de pouvoir modifier sa structure ultérieurement.
         * @param db type de base de données
         * @param oldVersion numéro de l'ancienne version à mettre à jour
         * @param newVersion numéro de la nouvelle version de la base de données
         */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		throw new UnsupportedOperationException("Not supported yet.");
	}
	
}
